package com.easy.facilitaterecycleadapter.adapter;

import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Sigthp-P-Department on 2018/5/7.
 */

public class WrapViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private RecyclerView.Adapter mAdapter;
    //存放头部底部集合
    private SparseArray<View> mHanders,mFooters;
    private int HANDER_KEY=1000000;
    private int FOOTER_KEY=2000000;
    //创建添加与移除的方法


    public WrapViewAdapter(RecyclerView.Adapter adapter){
        this.mAdapter=adapter;
        mHanders = new SparseArray<>();
        mFooters = new SparseArray<>();
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(mHanders.indexOfKey(viewType)>=0){
            return createHanderOrFooterView(mHanders.get(viewType));
        }
        if(mFooters.indexOfKey(viewType)>=0){
            return createHanderOrFooterView(mFooters.get(viewType));
        }
        return mAdapter.onCreateViewHolder(parent,viewType);
    }

    private RecyclerView.ViewHolder createHanderOrFooterView(View view) {
        return new RecyclerView.ViewHolder(view) {

        };
    }

    @Override
    public int getItemViewType(int position) {
        if(position<mHanders.size()){
            return mHanders.keyAt(position);
        }
        int abjPosition=position-mHanders.size();
        int adapterCount=0;
        if(mAdapter!=null){
            adapterCount=mAdapter.getItemCount();
            if(abjPosition<adapterCount){
                return mAdapter.getItemViewType(abjPosition);
            }
        }
        return mFooters.keyAt(abjPosition-adapterCount);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(position<mHanders.size()){
            return ;
        }
        int abjPosition=position-mHanders.size();
        int adapterCount=0;
        if(mAdapter!=null){
            adapterCount=mAdapter.getItemCount();
            if(abjPosition<adapterCount){
                mAdapter.onBindViewHolder(holder,abjPosition);
            }
        }
    }

    @Override
    public int getItemCount() {
        return mAdapter.getItemCount()+mHanders.size()+mFooters.size();
    }

    public void addHanderView(View view){
        if(mHanders.indexOfValue(view) < 0){
            mHanders.put(HANDER_KEY++,view);
            notifyDataSetChanged();
        }else{
        }
    }
    public void addFooterView(View view){
        if(mFooters.indexOfValue(view) <0){
            mFooters.put(FOOTER_KEY++,view);
            notifyDataSetChanged();
        }
    }
    public void removeHanderView(View view){
        if(mHanders.indexOfValue(view)>0){
            mHanders.removeAt(mHanders.indexOfValue(view));
            notifyDataSetChanged();
        }
    }
    public void removeFooterView(View view){
        if(mFooters.indexOfValue(view)>0){
            mFooters.removeAt(mFooters.indexOfValue(view));
            notifyDataSetChanged();
        }
    }
}
